package nju.mscs.server.businessLogic;

import java.util.Iterator;

import nju.mscs.data.object.*;

public class SC implements SearchController{
	Database db;
	public SC(){
		db=Controller.sumDB;
	}
	
	public String[] searchVIP(String one,String two){
		VipConsumer vip=null;
		if(!one.equals("")){
			vip=db.searchVipConsumerById(one);
		}
		else if(!two.equals(""))
			vip=db.searchVipConsumerByTelephone(Integer.parseInt(two.trim()));
		if(vip!=null){
		String sex=null;
		if(vip.getSex()==VipConsumer.MALE)
			sex="男";
		else
			sex="女";
		String time=vip.getBirth().getYear()+"."+vip.getBirth().getMonth()+"."+vip.getBirth().getDate();
		System.out.println(time);
		String[] result={vip.getId()+"",vip.getTelephone()+"",vip.getName(),sex,time,vip.getSumPoints()+"",""};
		System.out.println("\n\n"+"   "+result[0]+result[1]+result[2]);
		return result;
		}
		else
		return null;
	}

	
	public String[][] getUserList() {
		// TODO Auto-generated method stub
		Iterator<User> it=db.getUserList();
		int n=db.getAmountOfUser();
		System.out.println(n);
		String[][] result=new String[n][4];
		for(int i=0;i<n;i++){
			User user=it.next();
			result[i][0]=user.getId();
			result[i][1]=user.getName();
			result[i][3]=user.getPassword();
			String type=null;
			if(user.CASHIER==user.getType())
				type="收银员";
			if(user.SHOP_MANAGER==user.getType())
				type="分店经理";
			if(user.GENERAL_MANAGER==user.getType())
				type="总经理";
			if(user.SYSTEM_MANAGER==user.getType())
				type="系统管理员";
			result[i][2]=type;
			System.out.println("AAA:"+type);
		}
		return result;
	}

	@Override
	public String[][] getGoodsList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String[][] getGiftList() {
		// TODO Auto-generated method stub
		Iterator<Gift> ig=db.getGiftList();
		int n=0;
		while(ig.hasNext()){
			ig.next();
			n++;
		}
		ig=db.getGiftList();
		int i=0;
		String[][] result=new String[n][2];
		while(ig.hasNext()){
			Gift gift=ig.next();
			result[i][0]=gift.getId();
			result[i][1]=gift.getName();
			i++;
		}
		return result;
	}

	@Override
	public String[] getGoods(String id) {
		// TODO Auto-generated method stub
		Goods good=db.searchGoodsById(id);
		String[] result=null;
		if(good!=null){
			result=new String[4];
			result[0]=good.getId();
			result[1]=good.getName();
			result[2]=good.getPrice()+"";
			result[3]=good.getAmount()+"";
		}
		return result;
	}

	@Override
	public String[][] getPromotionList() {
		// TODO Auto-generated method stub
		Iterator<Promotion> ip=db.getPromotionList();
		int i=0;
		while(ip.hasNext()){
			i++;
			ip.next();
		}
		ip=db.getPromotionList();
		String[][] result=new String[i][8];
		for(int j=0;j<i;j++){
			Promotion p=ip.next();
			boolean bool1=(p.getType()== Promotion.GoodsDiscountPromotion);
			boolean bool2=(p.getType()== Promotion.GoodsGiftPromotion);
			boolean bool3=(p.getType()== Promotion.TotalDiscountPromotion);
			boolean bool4=(p.getType()== Promotion.TotalGiftPromotion);
			result[i][0]=p.getId();
			result[i][1]=p.getType()+"";
			if(bool3||bool4)
				result[i][2]=((TotalPromotion)p).getTotal();
			if(bool1||bool3)
				result[i][3]=((DiscountPromotion)p).getDiscount()+"";
			if(bool1||bool2)
				result[i][4]=((GoodsPromotion)p).getGoods().getId();
			if(bool3||bool4)
				result[i][5]=((GiftPromotion)p).getGift().getId();
			result[i][6]=p.getBeginTime().print();
			result[i][7]=p.getOverTime().print();
		}
		return result;
	}

	@Override
	public String[] getGift(String id) {
		
		// TODO Auto-generated method stub
		Gift gift=db.searchGiftById(id);
		if(gift!=null){
			String result[]={gift.getId(),gift.getName()};
			return result;
		}
		return null;
	}
}
